package com.bm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bm.Do.Epidemic.EpidemicDetailSqlQuery;
import com.bm.Do.Epidemic.EpidemicListSqlQuery;
import com.bm.Result.CodeEnum;
import com.bm.Result.Result;
import com.bm.model.EjyyEpidemic;
import com.bm.query.Epidemic.*;
import com.bm.service.EjyyEpidemicService;
import com.bm.mapper.EjyyEpidemicMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*
*/
@Service
public class EjyyEpidemicServiceImpl extends ServiceImpl<EjyyEpidemicMapper, EjyyEpidemic>
implements EjyyEpidemicService{


    private EjyyEpidemicMapper ejyyEpidemicMapper;


    @Override
    public Result createEpidemicMsg(EpidemicCreateQuery epidemicCreateQuery) {
        // 创建Epidemic对象
        EjyyEpidemic ejyyEpidemic = new EjyyEpidemic();


        //获取三个id
        Integer wechat_mp_user_id = epidemicCreateQuery.getWechat_mp_user_id();
        Integer building_id = epidemicCreateQuery.getBuilding_id();
        Integer community_id = epidemicCreateQuery.getCommunity_id();

        //将id都添加进epidemic中
        ejyyEpidemic.setWechatMpUserId(Long.valueOf(wechat_mp_user_id));
        ejyyEpidemic.setBuildingId(building_id.longValue());
        ejyyEpidemic.setCommunityId(community_id.longValue());
        //调用selectId，查询是否存在该用户
        EjyyEpidemic ejyyEpidemicResult = ejyyEpidemicMapper.selectId(ejyyEpidemic);

        //如果selectId查询没有该用户或用户不匹配
        if(ejyyEpidemicResult == null){
            return Result.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
        }

        //selectId查询后存在该用户,添加该用户的疫情防控信息
        int id = ejyyEpidemicMapper.addEpidemic(epidemicCreateQuery);

        //用户疫情防控信息添加成功
        if(id > 0){

        }else{
            //用户疫情防控信息添加失败
            return Result.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
        }

        return Result.OK(CodeEnum.OK);

    }

    @Override
    public Result listEpidemic(EpidemicListQuery epidemicListQuery) {
        Map<String,Object> where = new HashMap<>();

        // 判断tour_code是否为空
        if (epidemicListQuery.getTour_code() != null){
            where.put("ejyy_epidemic.tour_code",epidemicListQuery.getTour_code());
        }
        // 判断用户是否在家
        if (epidemicListQuery.getReturn_hometown() != null){
            where.put("ejyy_epidemic.return_hometown", epidemicListQuery.getReturn_hometown());
        }

        //接收数据库返回的数据
        List<EpidemicListSqlQuery> list = new ArrayList<>();
        //计算偏移量
        epidemicListQuery.setOffset( (epidemicListQuery.getPage_num()-1) * epidemicListQuery.getPage_size());

        //查询到的数据依次添加进list集合中
        EpidemicListSqlQuery[] epidemicListSqlQuery = ejyyEpidemicMapper.selectAll(epidemicListQuery);
        for (int i = 0; i < epidemicListSqlQuery.length; i++) {
            list.add(epidemicListSqlQuery[i]);
        }
        //总共查询到的数据量,暂时不知道怎么放到list集合中...
        int foundRows = ejyyEpidemicMapper.foundRows();


        return Result.OK(list);
    }

    @Override
    public Result detailEpidemic(EpidemicDetailQuery epidemicDetailQuery) {
        List<EpidemicDetailSqlQuery> list = new ArrayList<>();

        list.add(ejyyEpidemicMapper.selectById(epidemicDetailQuery));

        // 如果listg为空，查询不到对应id的数据
        if(list.isEmpty()){
            // 返回错误
            return Result.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
        }
        return Result.OK(list);
    }
}
